package labox.innovation.gameserver.scripting;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastMap;
import labox.innovation.config.Config;

/**
 * Cache of Compiled Scripts
 * 
 * @author  KenM
 */
public class CompiledScriptCache implements Serializable {
	/**
	 * Version 1
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger LOG = LoggerFactory.getLogger(CompiledScriptCache.class.getName());

	private Map<String, CompiledScriptHolder> _compiledScriptCache = new FastMap<String, CompiledScriptHolder>();

	private transient boolean _modified = false;

	public CompiledScript loadCompiledScript(ScriptEngine engine, File file) throws FileNotFoundException, ScriptException {
		int len = L2ScriptEngineManager.SCRIPT_FOLDER.getPath().length() + 1;
		String relativeName = file.getPath().substring(len);

		CompiledScriptHolder csh = _compiledScriptCache.get(relativeName);
		if (csh != null && csh.matches(file)) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("Reusing cached compiled script: {" + file.getName() +"}" , Config.SERVER_ID, "脚本引擎");
			}
			return csh.getCompiledScript();
		} else {
			if (Config.DEBUG) {
				LOG.info("Compiling script: " + file.getName(), Config.SERVER_ID, "脚本引擎");
			}
			Compilable eng = (Compilable) engine;
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

			// TODO lock file
			CompiledScript cs = eng.compile(reader);
			if (cs instanceof Serializable) {
				synchronized (_compiledScriptCache) {
					_compiledScriptCache.put(relativeName, new CompiledScriptHolder(cs, file));
					_modified = true;
				}
			}

			return cs;
		}
	}

	public boolean isModified() {
		return _modified;
	}

	public void purge() {
		synchronized (_compiledScriptCache) {
			for (String path : _compiledScriptCache.keySet()) {
				File file = new File(L2ScriptEngineManager.SCRIPT_FOLDER, path);
				if (!file.isFile()) {
					_compiledScriptCache.remove(path);
					_modified = true;
				}
			}
		}
	}

	public void save() throws FileNotFoundException, IOException {
		synchronized (_compiledScriptCache) {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(L2ScriptEngineManager.SCRIPT_FOLDER, "CompiledScripts.cache")));
			oos.writeObject(this);
			_modified = false;
		}
	}
}
